/*
 * DoAnJava3View.java
 */
package doanjava3;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.event.TableModelEvent;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListModel;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

/**
 * The application's main frame.
 */
public class DoAnJava3View extends FrameView
{
    //ten duong dan den file can doc/ghi

    private String pathFile = "c:\\file.bat";
    //ten cua bang moi duoc tao
    String name = "";
    //list chua danh sach cac bang o che do design
    private ArrayList<JTable> lstTBDes = new ArrayList<JTable>();
    //list chua danh sach cac bang o che do open
    private ArrayList<JTable> lstTBOpe = new ArrayList<JTable>();
    //list chua danh sach cac scrollpane cua moi tabbedpanel
    private ArrayList<JScrollPane> lstCrollP = new ArrayList<JScrollPane>();
    //list chua tinh trang cac bang co duoc luu hay khong (true/false)
    private ArrayList<Boolean> lstIsSaved = new ArrayList<Boolean>();
    //lay ve model cua mainList de lam viec voi cac item cua no
    DefaultListModel modelList = new DefaultListModel();

    public DoAnJava3View(SingleFrameApplication app) throws FileNotFoundException, IOException, ClassNotFoundException
    {
        super(app);

        initComponents();

        //lay ve danh sach cac bang design tu file
        lstTBDes = getListTableFromFile(pathFile);
        //lay ve danh sach cac bang open tu file

        //hien thi cac bang design len tab panel va list
        viewListTable(lstTBDes);
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++)
        {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener()
        {

            public void propertyChange(java.beans.PropertyChangeEvent evt)
            {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName))
                {
                    if (!busyIconTimer.isRunning())
                    {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                }
                else if ("done".equals(propertyName))
                {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                }
                else if ("message".equals(propertyName))
                {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                }
                else if ("progress".equals(propertyName))
                {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox()
    {
        if (aboutBox == null)
        {
            JFrame mainFrame = DoAnJava3App.getApplication().getMainFrame();
            aboutBox = new DoAnJava3AboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        DoAnJava3App.getApplication().show(aboutBox);
    }

    private JTable createTableDesign()
    {
        //        tbdesign.setModel((DefaultTableModel) createTableDesign().getModel());
        JTable tbdesign = new JTable(1, 2);
        tbdesign.getColumnModel().getColumn(0).setHeaderValue("Field Name");
        tbdesign.getColumnModel().getColumn(1).setHeaderValue("Data Type");
        //        tbdesign.getColumnModel().getColumn(0).setPreferredWidth(190);
        //        tbdesign.getColumnModel().getColumn(1).setPreferredWidth(120);
        JComboBox com = new JComboBox();
        //them combobox kieu du lieu cua cot vao bang moi tao
        com.addItem("Integer");
        com.addItem("String");
        DefaultCellEditor cell = new DefaultCellEditor(com);
        tbdesign.getColumnModel().getColumn(1).setCellEditor(cell);
        return tbdesign;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        mainTabP = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        mainList = new javax.swing.JList();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        DatabaseMenu = new javax.swing.JMenu();
        CreateDatabaseMenuItem = new javax.swing.JMenuItem();
        DeleteDatabaseMenuItem = new javax.swing.JMenuItem();
        TableMenu = new javax.swing.JMenu();
        CreateTableMenuItem = new javax.swing.JMenuItem();
        DeleteTableMenuItem = new javax.swing.JMenuItem();
        SaveTableMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        mainTabP.setName("mainTabP"); // NOI18N
        mainTabP.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mainTabPMouseClicked(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        mainList.setName("mainList"); // NOI18N
        mainList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mainListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(mainList);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainTabP, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainTabP, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doanjava3.DoAnJava3App.class).getContext().getResourceMap(DoAnJava3View.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doanjava3.DoAnJava3App.class).getContext().getActionMap(DoAnJava3View.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setIcon(resourceMap.getIcon("exitMenuItem.icon")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        exitMenuItem.setPressedIcon(resourceMap.getIcon("exitMenuItem.pressedIcon")); // NOI18N
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setIcon(resourceMap.getIcon("aboutMenuItem.icon")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        DatabaseMenu.setText(resourceMap.getString("DatabaseMenu.text")); // NOI18N
        DatabaseMenu.setName("DatabaseMenu"); // NOI18N

        CreateDatabaseMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.ALT_MASK));
        CreateDatabaseMenuItem.setText(resourceMap.getString("CreateDatabaseMenuItem.text")); // NOI18N
        CreateDatabaseMenuItem.setName("CreateDatabaseMenuItem"); // NOI18N
        CreateDatabaseMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CreateDatabaseMenuItemActionPerformed(evt);
            }
        });
        DatabaseMenu.add(CreateDatabaseMenuItem);

        DeleteDatabaseMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.ALT_MASK));
        DeleteDatabaseMenuItem.setText(resourceMap.getString("DeleteDatabaseMenuItem.text")); // NOI18N
        DeleteDatabaseMenuItem.setName("DeleteDatabaseMenuItem"); // NOI18N
        DeleteDatabaseMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteDatabaseMenuItemActionPerformed(evt);
            }
        });
        DatabaseMenu.add(DeleteDatabaseMenuItem);

        menuBar.add(DatabaseMenu);

        TableMenu.setText(resourceMap.getString("TableMenu.text")); // NOI18N
        TableMenu.setName("TableMenu"); // NOI18N

        CreateTableMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        CreateTableMenuItem.setIcon(resourceMap.getIcon("CreateTableMenuItem.icon")); // NOI18N
        CreateTableMenuItem.setText(resourceMap.getString("CreateTableMenuItem.text")); // NOI18N
        CreateTableMenuItem.setName("CreateTableMenuItem"); // NOI18N
        CreateTableMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CreateTableMenuItemActionPerformed(evt);
            }
        });
        TableMenu.add(CreateTableMenuItem);

        DeleteTableMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        DeleteTableMenuItem.setIcon(resourceMap.getIcon("DeleteTableMenuItem.icon")); // NOI18N
        DeleteTableMenuItem.setText(resourceMap.getString("DeleteTableMenuItem.text")); // NOI18N
        DeleteTableMenuItem.setName("DeleteTableMenuItem"); // NOI18N
        DeleteTableMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteTableMenuItemActionPerformed(evt);
            }
        });
        TableMenu.add(DeleteTableMenuItem);

        SaveTableMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        SaveTableMenuItem.setIcon(resourceMap.getIcon("SaveTableMenuItem.icon")); // NOI18N
        SaveTableMenuItem.setText(resourceMap.getString("SaveTableMenuItem.text")); // NOI18N
        SaveTableMenuItem.setName("SaveTableMenuItem"); // NOI18N
        SaveTableMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveTableMenuItemActionPerformed(evt);
            }
        });
        TableMenu.add(SaveTableMenuItem);

        menuBar.add(TableMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 708, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 538, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

private void CreateDatabaseMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CreateDatabaseMenuItemActionPerformed
// TODO add your handling code here:
    String nameDB = JOptionPane.showInputDialog(null, "Enter Database's Name", "New Database", 1);

    if (nameDB == null)
        return;
    else if ("".equals(nameDB))
        JOptionPane.showMessageDialog(null, "Database Name can't null\r\nCreate Database faill.", "Error", 0);
    else
    {
    }
}//GEN-LAST:event_CreateDatabaseMenuItemActionPerformed

private void DeleteDatabaseMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteDatabaseMenuItemActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_DeleteDatabaseMenuItemActionPerformed

private void CreateTableMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CreateTableMenuItemActionPerformed
// TODO add your handling code here:
    name = JOptionPane.showInputDialog(null, "Enter Table's Name", "New Table", 1);

    if (name == null)
        return;
    else if ("".equals(name))
        JOptionPane.showMessageDialog(null, "Table Name can't null\r\nCreate Table faill.", "Error", 0);
    else
    {
        //tao 1 bang moi o dang thiet ke
        final JTable tbdesign = createTableDesign();
        tbdesign.setName(name + "Design");
        final DefaultTableModel modeldesign = (DefaultTableModel) tbdesign.getModel();
        //bang tbdesign bat su kien click vao dong cuoi cung thi phat sinh them dong moi
        tbdesign.addMouseListener(new java.awt.event.MouseAdapter()
        {

            @Override
            public void mouseClicked(java.awt.event.MouseEvent e)
            {
                if (tbdesign.getSelectedRow() == tbdesign.getRowCount() - 1)
                {
                    modeldesign.addRow(new Object[2]);
                }
            }
        });
        //tbdesign lang nghe su kien thay doi gia tri de danh giau la ko luu cho list tinh trang
        tbdesign.getModel().addTableModelListener(new TableModelListener()
        {

            public void tableChanged(TableModelEvent e)
            {
                lstIsSaved.set(mainTabP.getSelectedIndex(), Boolean.FALSE);
            }
        });
        //tao 1 bang rong moi o dang nhap lieu
//        final JTable tbopen = new JTable();
//        tbopen.setName(name + "Design");
//        tbopen.setModel((DefaultTableModel) createTableOpen().getModel());
//        final DefaultTableModel modelopen = (DefaultTableModel)tbopen.getModel();
//        tbopen.addMouseListener(new java.awt.event.MouseAdapter()
//        {
//
//            @Override
//            public void mouseClicked(java.awt.event.MouseEvent e)
//            {
//                if (tbopen.getSelectedRow() == tbopen.getRowCount() - 1)
//                {
//                    modelopen.addRow(new Object[2]);
//                }
//            }
//        });
        //them bang vua tao vao danh sach cac bang thiet ke
        lstTBDes.add(tbdesign);
        //them bang vua tao vao danh sach cac bang nhap lieu
        //them item moi cho mainList
        modelList.add(modelList.getSize(), name);
        mainList.setModel(modelList);
        //them tab moi cho tabpanel
        JScrollPane scrollpane = new JScrollPane();
        scrollpane.setName(name + "ScrollPane");
        scrollpane.setViewportView(tbdesign);
        lstCrollP.add(scrollpane);
        mainTabP.add(name, scrollpane);
        //cho con tro chuot chon bang vua tao
        mainList.setSelectedIndex(modelList.size() - 1);
        mainTabP.setSelectedIndex(mainTabP.getTabCount() - 1);
        //them tinh trang da luu (doi voi bang vua tao) vao danh sach tinh trang (save/don't save)
        lstIsSaved.add(true);
    }

}//GEN-LAST:event_CreateTableMenuItemActionPerformed

private void DeleteTableMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteTableMenuItemActionPerformed
// TODO add your handling code here:
    //lay ve tat ca vi tri cac item duoc chon
    int[] Selecteds = mainList.getSelectedIndices();
    for (int i = Selecteds.length; i > 0; i--)
    {
        //xoa cac item duoc chon trong mainList
        modelList.remove(Selecteds[i - 1]);
        //xoa cac tabPane tuong ung
        mainTabP.remove(Selecteds[i - 1]);
        //xoa cac tinh trang tuong ung cua bang vua xoa
        lstIsSaved.remove(Selecteds[i - 1]);
    }
}//GEN-LAST:event_DeleteTableMenuItemActionPerformed

private void mainListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainListMouseClicked
// TODO add your handling code here:
    //neu co chon 1 item trem mainList
    if (mainList.getSelectedIndex() != -1)
    {
        //kiem tra xem bang truoc do da luu hay chua
        if (mainTabP.getSelectedIndex() != mainList.getSelectedIndex())
        {
            //neu nhu co thao tac lam thay doi gia tri cua bang ma chua luu lai
            if (lstIsSaved.get(mainTabP.getSelectedIndex()) == false)
            {
                int result = JOptionPane.showConfirmDialog(null, "Do you save " + name + "'s Table?", "Save Table?", 0, 3);
                if (result == JOptionPane.YES_OPTION)
                {
                    //cap nhat lai dua lieu, goi su kien khi nhan nut save table
                    SaveTableMenuItemActionPerformed(null);
                }
                else
                {
                    lstIsSaved.set(mainTabP.getSelectedIndex(), Boolean.TRUE);
//                    lstCrollP.get(mainTabP.getSelectedIndex()).setViewportView(lstTBDes.get(mainTabP.getSelectedIndex()));
//                    JScrollPane crollpane = (JScrollPane)mainTabP.getComponent(mainTabP.getSelectedIndex());
//                    crollpane.setViewportView(lstTBDes.get(mainTabP.getSelectedIndex()-1));
                }
            }
        }
        //cho tabPanel tuong ung cung chon theo luon
        mainTabP.setSelectedIndex(mainList.getSelectedIndex());

    }
}//GEN-LAST:event_mainListMouseClicked

private void mainTabPMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainTabPMouseClicked
// TODO add your handling code here:
    if (mainTabP.getSelectedIndex() != -1)
    {
        mainList.setSelectedIndex(mainTabP.getSelectedIndex());
    }
}//GEN-LAST:event_mainTabPMouseClicked

private void SaveTableMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveTableMenuItemActionPerformed
// TODO add your handling code here:
    //danh dau bang dang mo tren tabpanel da duoc luu (true)
    lstIsSaved.set(mainTabP.getSelectedIndex(), Boolean.TRUE);
    //cap nhat lai table design dang duoc chon o tab panel

}//GEN-LAST:event_SaveTableMenuItemActionPerformed

private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
// TODO add your handling code here:
    //ghi danh sach cac bang design vao file
    writeListTableToFile(lstTBDes, pathFile);
    //ghi danh sach cac bang open vao file
}//GEN-LAST:event_exitMenuItemActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem CreateDatabaseMenuItem;
    private javax.swing.JMenuItem CreateTableMenuItem;
    private javax.swing.JMenu DatabaseMenu;
    private javax.swing.JMenuItem DeleteDatabaseMenuItem;
    private javax.swing.JMenuItem DeleteTableMenuItem;
    private javax.swing.JMenuItem SaveTableMenuItem;
    private javax.swing.JMenu TableMenu;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList mainList;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTabbedPane mainTabP;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;

    private JTable createTableOpen()
    {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void writeListTableToFile(ArrayList<JTable> lsttbdes, String pathfile)
    {
        clsTable clstable = new clsTable();
        clstable.writeListTableToFile(lsttbdes, pathfile);
    }

    private ArrayList<JTable> getListTableFromFile(String pathfile) throws FileNotFoundException, IOException, ClassNotFoundException
    {
        clsTable clstable = new clsTable();
        return clstable.getListTableFromFile(pathfile);
    }

    private void viewListTable(ArrayList<JTable> lsttbdes)
    {
        String[] strName = new String[lsttbdes.size()];
        String strname = "";
        for (int i = 0; i < lsttbdes.size(); i++)
        {
            strName[i] = lsttbdes.get(i).getName();
            strname = strName[i].substring(0, strName[i].length() - 6);
            modelList.add(modelList.getSize(), strname);
            mainList.setModel(modelList);
            //them tab moi cho tabpanel
            JScrollPane scrollpane = new JScrollPane();
            scrollpane.setName(strname + "ScrollPane");
            scrollpane.setViewportView(lsttbdes.get(i));
            lstCrollP.add(scrollpane);
            mainTabP.add(strname, scrollpane);
            //cho con tro chuot chon bang vua tao
            mainList.setSelectedIndex(modelList.size() - 1);
            mainTabP.setSelectedIndex(mainTabP.getTabCount() - 1);
            //them tinh trang da luu (doi voi bang vua tao) vao danh sach tinh trang (save/don't save)
            lstIsSaved.add(true);
        }
    }
}

class clsTable
{

    void writeListTableToFile(ArrayList<JTable> lsttbdes, String pathfile)
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(pathfile);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            for (int i = 0; i < lsttbdes.size(); i++)
            {
                oos.writeObject(lsttbdes.get(i));
                oos.reset();
            }
            fos.close();
        } catch (Exception e)
        {
        }
        System.out.println(lsttbdes.size());
    }

    ArrayList<JTable> getListTableFromFile(String pathfile) throws FileNotFoundException, IOException, ClassNotFoundException
    {
        ArrayList<JTable> lsttable = new ArrayList<JTable>();
        FileInputStream fis = new FileInputStream(pathfile);
        ObjectInputStream ois = new ObjectInputStream(fis);
        try
        {
            while(true){
            JTable table = (JTable)ois.readObject();
            if(table != null){
                lsttable.add(table);
            }
            else
                break;
        }
        fis.close();
        ois.close();
        } catch (EOFException eof)
        {
            System.out.println("EOFException");
        }catch(ClassNotFoundException dlc){
            System.out.println("ClassNotFoundException");
        }catch(FileNotFoundException fil){
            System.out.println("FileNotFoundException");
        }catch(IOException io){
            System.out.println("ioexception");
        }       
        return lsttable;
    }
}
